home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-portable.exe / quodlibet-3.3.0-portable / data / bin / fnmatch.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  3KB  |  126 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. '''Filename matching with shell patterns.
  5.  
  6. fnmatch(FILENAME, PATTERN) matches according to the local convention.
  7. fnmatchcase(FILENAME, PATTERN) always takes case in account.
  8.  
  9. The functions operate by translating the pattern into a regular
  10. expression.  They cache the compiled regular expressions for speed.
  11.  
  12. The function translate(PATTERN) returns a regular expression
  13. corresponding to PATTERN.  (It does not compile it.)
  14. '''
  15. import re
  16. __all__ = [
  17.     'filter',
  18.     'fnmatch',
  19.     'fnmatchcase',
  20.     'translate']
  21. _cache = { }
  22. _MAXCACHE = 100
  23.  
  24. def _purge():
  25.     '''Clear the pattern cache'''
  26.     _cache.clear()
  27.  
  28.  
  29. def fnmatch(name, pat):
  30.     """Test whether FILENAME matches PATTERN.
  31.  
  32.     Patterns are Unix shell style:
  33.  
  34.     *       matches everything
  35.     ?       matches any single character
  36.     [seq]   matches any character in seq
  37.     [!seq]  matches any char not in seq
  38.  
  39.     An initial period in FILENAME is not special.
  40.     Both FILENAME and PATTERN are first case-normalized
  41.     if the operating system requires it.
  42.     If you don't want this, use fnmatchcase(FILENAME, PATTERN).
  43.     """
  44.     import os as os
  45.     name = os.path.normcase(name)
  46.     pat = os.path.normcase(pat)
  47.     return fnmatchcase(name, pat)
  48.  
  49.  
  50. def filter(names, pat):
  51.     '''Return the subset of the list NAMES that match PAT'''
  52.     import os
  53.     import posixpath as posixpath
  54.     result = []
  55.     pat = os.path.normcase(pat)
  56.     if pat not in _cache:
  57.         res = translate(pat)
  58.         if len(_cache) >= _MAXCACHE:
  59.             _cache.clear()
  60.         _cache[pat] = re.compile(res)
  61.     match = _cache[pat].match
  62.     if os.path is posixpath:
  63.         for name in names:
  64.             if match(name):
  65.                 result.append(name)
  66.                 continue
  67.     for name in names:
  68.         if match(os.path.normcase(name)):
  69.             result.append(name)
  70.             continue
  71.     return result
  72.  
  73.  
  74. def fnmatchcase(name, pat):
  75.     """Test whether FILENAME matches PATTERN, including case.
  76.  
  77.     This is a version of fnmatch() which doesn't case-normalize
  78.     its arguments.
  79.     """
  80.     if pat not in _cache:
  81.         res = translate(pat)
  82.         if len(_cache) >= _MAXCACHE:
  83.             _cache.clear()
  84.         _cache[pat] = re.compile(res)
  85.     return _cache[pat].match(name) is not None
  86.  
  87.  
  88. def translate(pat):
  89.     '''Translate a shell PATTERN to a regular expression.
  90.  
  91.     There is no way to quote meta-characters.
  92.     '''
  93.     i = 0
  94.     n = len(pat)
  95.     res = ''
  96.     while i < n:
  97.         c = pat[i]
  98.         i = i + 1
  99.         if c == '*':
  100.             res = res + '.*'
  101.             continue
  102.         if c == '?':
  103.             res = res + '.'
  104.             continue
  105.         if c == '[':
  106.             j = i
  107.             if j < n and pat[j] == '!':
  108.                 j = j + 1
  109.             if j < n and pat[j] == ']':
  110.                 j = j + 1
  111.             while j < n and pat[j] != ']':
  112.                 j = j + 1
  113.             if j >= n:
  114.                 res = res + '\\['
  115.             else:
  116.                 stuff = pat[i:j].replace('\\', '\\\\')
  117.                 i = j + 1
  118.                 if stuff[0] == '!':
  119.                     stuff = '^' + stuff[1:]
  120.                 elif stuff[0] == '^':
  121.                     stuff = '\\' + stuff
  122.                 res = '%s[%s]' % (res, stuff)
  123.         res = res + re.escape(c)
  124.     return res + '\\Z(?ms)'
  125.  
  126.